# coding:utf-8

import pandas as pd
import os
import PyPDF2


pdir = r'D:\下载\华美'
df = pd.read_excel(os.path.join(pdir, '谁房价最低.xlsx'))
df = df[df['月份'] == '4月']
# print(df.shape)


def hb(p1, p2, hotel):
    output = PyPDF2.PdfFileWriter()
    outputPages = 0
    input1 = PyPDF2.PdfFileReader(open(p1, 'rb'))
    num = input1.getNumPages()
    for i in range(num):
        output.addPage(input1.getPage(i))

    input2 = PyPDF2.PdfFileReader(open(p2, 'rb'))
    num = input2.getNumPages()
    for i in range(1, num):
        output.addPage(input2.getPage(i))

    output_p = open(os.path.join(pdir, f'合并后的pdf/{hotel}报告.pdf'), "wb")
    output.write(output_p)
    output_p.close()
    print(hotel, "-----PDF文件合并完成！")


for item in df.itertuples():
    hotel = item[1]
    pdf_p = os.path.join(pdir, f'合并后的pdf/{hotel}报告.pdf')
    if os.path.exists(pdf_p):
        continue
    p1 = os.path.join(pdir, f'pb生产的pdf文件-排名/{hotel}排名.pdf')
    p2 = os.path.join(pdir, f'pb生产的pdf文件-趋势/{hotel}趋势.pdf')
    if os.path.exists(p1) and os.path.exists(p2):
        hb(p1, p2, hotel)

print('全部合并完成！！！！！！！！！！！')
